package com.zt.douyamusic.activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.zt.douyamusic.R;
import com.zt.douyamusic.bmob.UserBean;
import com.zt.douyamusic.utils.CallPhoneUtils;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class LoginActivity extends InfoBaseActivity {
    EditText mobileEdit,passEdit;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mobileEdit = (EditText) findViewById(R.id.mobile);
        passEdit = (EditText) findViewById(R.id.pass);
        findViewById(R.id.login_tv).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                String mobile = mobileEdit.getText().toString().trim();
                String password = passEdit.getText().toString().trim();
                if (!CallPhoneUtils.isPhone(mobile)){
                    Toast.makeText(LoginActivity.this, "手机号码不正确", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)){
                    Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                final UserBean user = new UserBean();
                //此处替换为你的用户名
                user.setUsername(mobile);
                //此处替换为你的密码
                user.setPassword(password);
                user.login(new SaveListener<UserBean>() {
                    @Override
                    public void done(UserBean bmobUser, BmobException e) {
                        if (e == null) {
                            //登录成功
                            finish();
                        } else {
                            //登录失败
                            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
        findViewById(R.id.register_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
        findViewById(R.id.back_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
